﻿namespace RazorEngine
{
    using System;
    using System.Collections.Concurrent;
    using System.Collections.Generic;

    using Compilation;
    using Configuration;
    using Templating;
    using TSharp.Core;
    using TSharp.Core.Services;

    /// <summary>
    /// Provides quick access to template services.
    /// </summary>
    public static class Razor
    {
        #region Methods

        private static ITemplateParserService DefaultTemplateService
        {
            get
            {
                return AopContext.Services.GetThread().Get<ITemplateParserService>();
            }
        }

        /// <summary>
        /// Pre-compiles the specified template and caches it using the specified name.
        /// </summary>
        /// <param name="template">The template to precompile.</param>
        /// <param name="templateBaseType">Type of the template base.</param>
        /// <param name="modelType">The type of model used in the template.</param>
        /// <param name="name">The cache name for the template.</param>
        public static void Compile(string template, Type modelType, string name, Type templateBaseType)
        {
            DefaultTemplateService.CompileType(template, name, modelType, templateBaseType);
        }

         
        /// <summary>
        /// Parses the given template and returns the result.
        /// </summary>
        /// <typeparam name="T">The model type.</typeparam>
        /// <param name="template">The template to parse.</param>
        /// <param name="model">The model.</param>
        /// <param name="templateBaseType">Type of the template base.</param>
        /// <param name="name">[Optional] The name of the template. This is used to cache the template.</param>
        /// <returns>
        /// The string result of the parsed template.
        /// </returns>
        public static string Parse<T>(string template, T model, Type templateBaseType = null, string name = null)
        {
            return DefaultTemplateService.Parse<T>(template, name, model, templateBaseType);
        }

        /// <summary>
        /// Runs the template with the specified name.
        /// </summary>
        /// <param name="name">The name of the template to run.</param>
        /// <returns>
        /// The result of the template.
        /// </returns>
        public static string Run(string name)
        {
            return DefaultTemplateService.Run(name);
        }

        /// <summary>
        /// Runs the template with the specified name.
        /// </summary>
        /// <typeparam name="T">The model type.</typeparam>
        /// <param name="model">The model.</param>
        /// <param name="name">The name of the template to run.</param>
        /// <returns>
        /// The result of the template.
        /// </returns>
        public static string Run<T>(T model, string name)
        {
            return DefaultTemplateService.Run<T>(model, name);
        }

        #endregion
    }
}